/**
 * 设置装车站基本传参
 * @author xiaoxujs
 */

import { Form, Input, InputNumber, message, Modal, Radio, Select, Switch } from "antd";
import { FC, useEffect, useState } from "react";
import { handleGetDictionaryItem } from "../../../../../assets/fun/DictionaryItem";
import { huopin } from "../../../../../assets/data";
import { IlplTLaneQueryLaneParamForm, ITlplTLaneLaneParamUpdateParams, ITlplTLaneQueryLaneParam } from "../../../../../type/remoteControl/loadingControlList";
import { postTlplTLaneLaneParamUpdate } from "../../../api";

const SetPlcModule: FC<{
  open: boolean,
  handleSetPlcCallback: () => void;
  clickData?: ITlplTLaneQueryLaneParam,
  clickId?: string;
}> = ({
  open,
  handleSetPlcCallback,
  clickData,
  clickId
}) => {

    const [form] = Form.useForm<IlplTLaneQueryLaneParamForm>();

    const [loading, setLoading] = useState(false);

    useEffect(() => {
      if (clickData) {
        let a = clickData.laneState === 0 ? true : false
        const data: IlplTLaneQueryLaneParamForm = {
          ...clickData,
          laneState: a,
        }
        form.setFieldsValue(data)
      }
    }, [clickData, form])
    const handleOk = async () => {
      const values = await form.validateFields();
      if (clickData && clickId) {
        setLoading(true);
        const params: ITlplTLaneLaneParamUpdateParams = {
          ...values,
          laneState: values.laneState ? 0 : 1,
          id: clickId
        }
        const { code, msg } = await postTlplTLaneLaneParamUpdate(params) as { code: number; msg: string };
        if (code === 20000) {
          form.resetFields();
          handleSetPlcCallback();
        } else {
          message.error(msg)
        }
        setLoading(false)

      }

    };

    const handleCancel = () => {
      handleSetPlcCallback();
    };
    return <Modal title="设置参数" confirmLoading={loading} open={open} onOk={handleOk} onCancel={handleCancel}>
      <Form
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 16 }}
        form={form}
        initialValues={{
          laneState: true, 
        }}
      >
        <Form.Item<IlplTLaneQueryLaneParamForm>
          label="车道名称"
          name="laneName"
          rules={[{ required: true, message: '请输入!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<IlplTLaneQueryLaneParamForm>
          label="使用状态"
          name="laneState"
          rules={[{ required: true, message: '请输入!' }]}
        >
          <Switch  />
        </Form.Item>
        <Form.Item<IlplTLaneQueryLaneParamForm>
          label="车道编号"
          name="laneNum"
          rules={[{ required: true, message: '请输入!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<IlplTLaneQueryLaneParamForm>
          label="最大限值(吨)"
          name="limitValue"
          rules={[{ required: true, message: '请输入限值!' }]}
        >
          <InputNumber style={{ width: '100%' }} min={0} max={100} />
        </Form.Item>
        <Form.Item<IlplTLaneQueryLaneParamForm>
          label="货品"
          name="product"
          rules={[{ required: true, message: '请选择!' }]}
        >
          <Select
            placeholder="请选择"
            style={{ width: 169 }}
            options={handleGetDictionaryItem(huopin)}
          />
        </Form.Item>
      </Form>
    </Modal>
  }

export default SetPlcModule;